﻿using Caliburn.Micro;
using NthDownload.Contracts;
using System.ComponentModel.Composition;
using NthDownload.Composition;
using NthDownload.Common.Validation;

namespace NthDownload.Common.ViewModels
{
    public class ViewModelScreenBase : Screen, IViewModel
    {
        #region Class Members

        private ViewModelResults _result;
        private bool _isBusy;
        private bool _importsSatisfied;
        private BindingGroupValidator _bindingGroupValidator;

        #endregion Class Members

        #region Constructor

        public ViewModelScreenBase()
        {
            //
            // Set Defaults
            //
            Result = ViewModelResults.None;
            BindingGroupValidator = new BindingGroupValidator();

            //
            // Satisfy imports 
            // 
            SatisfyImports();
        }

        #endregion Constructor

        #region Properties

        /// <summary>
        /// The default binding group validator
        /// </summary>
        public BindingGroupValidator BindingGroupValidator
        {
            get
            {
                return _bindingGroupValidator;
            }
            protected set
            {
                _bindingGroupValidator = value;
                NotifyOfPropertyChange(() => BindingGroupValidator);
            }
        }

        /// <summary>
        /// Indicates if the view model is busy
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }
            protected set
            {
                _isBusy = value;
                NotifyOfPropertyChange(() => IsBusy);
            }
        }

        /// <summary>
        /// Indicates if the view models imports have been satisfied
        /// </summary>
        public bool ImportsSatisfied
        {
            get
            {
                return _importsSatisfied;
            }
            private set
            {
                _importsSatisfied = value;
                NotifyOfPropertyChange(() => ImportsSatisfied);
            }
        }

        /// <summary>
        /// The result of the view model
        /// </summary>
        public ViewModelResults Result
        {
            get
            {
                return _result;
            }
            protected set
            {
                _result = value;
                NotifyOfPropertyChange(() => Result);
            }
        }

        /// <summary>
        /// The navigation service
        /// </summary>
        [Import]
        public INavigationService NavigationService { get; set; }

        /// <summary>
        /// The navigation service
        /// </summary>
        [Import]
        public IEventAggregator EventAggregator { get; set; }

        /// <summary>
        /// The window manager
        /// </summary>
        [Import]
        public IWindowManager WindowManager { get; set; }

        #endregion Properties

        #region Methods

        /// <Summary>
        /// Satisfies imports for the current view model
        /// </Summary>
        protected void SatisfyImports()
        {
            ImportsSatisfied = ImportService.TrySatisfyImports(this);
        }

        #endregion Methods
    }
}
